package com.yan.club.sys.control;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yan.club.result.Result;
import com.yan.club.sys.dto.SysDictTypeDto;
import com.yan.club.sys.model.SysDictType;
import com.yan.club.sys.service.SysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yan.club.auth.utils.SecurityUtil.getUser;
import static com.yan.club.result.Result.success;
import static com.yan.club.result.ResultCode.INTERNAL_SERVER_ERROR;

/**
 * @author 彦
 * @since 2024/2/7 15:15
 */
@RestController
@RequestMapping("/sys/dict/type")
public class SysDictTypeController {

    @Resource
    private SysDictTypeService sysDictTypeService;

//    @Resource
//    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询所有字典种类
     *
     * @param dictType
     * @return
     */
    @PreAuthorize("@check.hasAuthority('system:dict:list')")
    @GetMapping("/list")
    public Result<PageInfo<SysDictType>> list(SysDictTypeDto dictType) {
        PageHelper.startPage(dictType.getPageNum(), dictType.getPageSize(), true);
        List<SysDictType> sysDictTypes = sysDictTypeService.selectDictTypeList(dictType);
        return success(new PageInfo<>(sysDictTypes));
    }

    /**
     * 查询单个字典种类
     */
    @PreAuthorize("@check.hasAuthority('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public Result<SysDictType> getInfo(@PathVariable Long dictId) {
        return success(sysDictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@check.hasAuthority('system:dict:add')")
    @PostMapping
    public Result<String> add(@Validated @RequestBody SysDictType dict) {
        if (sysDictTypeService.checkDictTypeUnique(dict)) {
            return Result.error(INTERNAL_SERVER_ERROR.code,
                    "新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }

        dict.setCreateBy(getUser().getUserName());
        return sysDictTypeService.insertDictType(dict);
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@check.hasAuthority('system:dict:edit')")
    @PutMapping
    public Result<String> edit(@Validated @RequestBody SysDictType dict) {
        if (sysDictTypeService.checkDictTypeUnique(dict)) {
            return Result.error(INTERNAL_SERVER_ERROR.code,
                    "修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(getUser().getUserName());

        return sysDictTypeService.updateDictType(dict);

    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@check.hasAuthority('system:dict:remove')")
    @DeleteMapping("/{dictIds}")
    public Result<String> remove(@PathVariable Long[] dictIds) {
        sysDictTypeService.deleteDictTypeByIds(dictIds);
        return Result.success();
    }

//    /**
//     * 获取字典选择框列表
//     */
//    @GetMapping("/optionselect")
//    public Result optionselect()
//    {
//        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
//        return success(dictTypes);
//    }
}
